package cn.rht.study.util.redis;


import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.exceptions.JedisException;

import java.util.ArrayList;
import java.util.List;


public class JedisStringCache extends JedisCache {
	
	private String key;
	private int seconds=60*60*24*30;//30天
	
	public JedisStringCache( JedisPool pool, String key ){
		super( pool );
		this.key = key;
	}
	
    public long set(String value,int seconds ) {
    	return set( value,  seconds, false );
    }
    
    public long set(String value ) {
    	return set( value,  seconds, false );
    }

    /**
     * 
     * @param key
     * @param value
     * @param forceNewNotExist
     * @return 1-设置成功 ;0-值已存在
     */
    public long set(String value,int seconds , boolean forceNewNotExist) {
        Jedis jedis = null;
        try {
        	jedis = pool.getResource();
        	if( forceNewNotExist )
        		return jedis.setnx( key , value);
            jedis.set( key , value);
            if( seconds < 0){
        		return jedis.persist( key );
            }
            jedis.expire( key, seconds );
            return 1L;
        } catch (Exception e) {
            e.printStackTrace();
        	//pool.returnBrokenResource(jedis);
         	throw new JedisException(e.getMessage());
        } finally {
        	if(jedis!=null){
        		jedis.close();
        	}
            //pool.returnResource(jedis);
        }
    }
    
    public String get(){
    	Jedis jedis = null;
    	try {
        	jedis = pool.getResource();
        	String val = jedis.get(key);
            return val;
        } catch (Exception e) {
            e.printStackTrace();
        	//pool.returnBrokenResource(jedis);
         	throw new JedisException(e.getMessage());
        } finally {
        	if(jedis!=null){
        		jedis.close();
        	}
            //pool.returnResource(jedis);
        }		
    }
    


    public long incr(String key, long value) {
        Jedis jedis = null;
        try {
        	jedis = pool.getResource();
        	Long incrBy = jedis.incrBy(key, value);
    		return incrBy;
        } catch (Exception e) {
            e.printStackTrace();
        	//pool.returnBrokenResource(jedis);
         	throw new JedisException(e.getMessage());
        } finally {
        	if(jedis!=null){
        		jedis.close();
        	}
            //pool.returnResource(jedis);
        }
    }
    
    
    public long remove(String key) {
        Jedis jedis = (Jedis)pool.getResource();
        try
        {
            long delResult = jedis.del(new String[] { key });
            return delResult;
        } catch(Exception e) {
            e.printStackTrace();
            //pool.returnResource(jedis);
            throw new JedisException(e.getMessage());
        }finally{
        	if(jedis!=null){
        		jedis.close();
        	}
        	//pool.returnResource(jedis);
        }
    }
    
    /**
     * 设置过期时间 (秒)
     * @param key
     * @param seconds
     * @return
     */
    public long setTTLSeconds(int seconds ) {
        Jedis jedis = null;
        try {
        	jedis = pool.getResource();
        	if( seconds < 0) {
				Long persist = jedis.persist( key );
				return persist;
			}
        	Long expire = jedis.expire( key, seconds );
            return expire;
        } catch (Exception e) {
        	e.printStackTrace();
        	//pool.returnBrokenResource(jedis);
        	throw new JedisException(e.getMessage());
        } finally {
        	if(jedis!=null){
        		jedis.close();
        	}
            //pool.returnResource(jedis);
        }
    }
    
    public List<String> findLike(String like){
        Jedis jedis = null;
    	jedis = pool.getResource();
    	  // 游标初始值为0
        String cursor = ScanParams.SCAN_POINTER_START;
        String key = like+"*";
        ScanParams scanParams = new ScanParams();
        scanParams.match(key);// 匹配以 PLFX-ZZSFP-* 为前缀的 key
        scanParams.count(3000);
        List<String> list = new ArrayList<String>(6000);
        while (true){
            //使用scan命令获取数据，使用cursor游标记录位置，下次循环使用
            ScanResult<String> scanResult = jedis.scan(cursor, scanParams);
            cursor = scanResult.getStringCursor();// 返回0 说明遍历完成
            list.addAll(scanResult.getResult());
//			System.out.println(list.size());
//            long t2 = System.currentTimeMillis();
//            System.out.println("获取" + list.size()
//                + "条数据，耗时: " + (t2-t1) + "毫秒,cursor:" + cursor);
            if ("0".equals(cursor)){
                break;
            }
        }
        
        return list;
    }
}








